﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SchoolManagementSystem.Classes;
using SchoolManagementSystem.Exceptions;

namespace SchoolManagementSystem.Forms
{
    public partial class SelectForm : Form
    {
        class Item
        {
            public string column;
            public CheckBox checkBox;
            public System.Windows.Forms.TextBox textBox;
            public Item(CheckBox checkBox, string column, System.Windows.Forms.TextBox textBox)
            {
                this.checkBox = checkBox;
                this.column = column;
                this.textBox = textBox;
            }
        }

        private ParentForm parentForm;
        private SQLiteConnecter sqliteConnecter;
        private int radioChoosen = 0;
        private List<Item> listCheckBoxSchool;
        private List<Item> listCheckBoxClass;
        private List<Item> listCheckBoxStudent;

        public SelectForm(ParentForm parentForm, SQLiteConnecter connecter)
        {
            InitializeComponent();
            this.parentForm = parentForm;
            this.MdiParent = parentForm;
            this.Dock = DockStyle.Fill;
            this.sqliteConnecter = connecter;
            checkBoxListInit();
        }

        /// <summary>
        /// 点击查询按钮，执行查询操作，
        /// 包括包装窗口信息、
        /// 调用SqliteConnecter的方法、
        /// 调用本类方法更新消息显示表的方法显示查找结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            string table = "";
            List<string> columns = new List<string>();
            List<KeyValuePair<string,string>> conditions = new List<KeyValuePair<string, string>>();
            List<Item> getList;
            switch (radioChoosen)
            {
                case 0: 
                    table = "schools"; getList = listCheckBoxSchool;
                    columns.Add("school_id");
                    columns.Add("school_name");
                    break;
                case 1: 
                    table = "classes"; getList = listCheckBoxClass;
                    columns.Add("class");
                    break;
                case 2:
                default: 
                    table = "students"; getList = listCheckBoxStudent;
                    columns.Add("stu_id");
                    columns.Add("stu_name");
                    break;
            }
            foreach (Item item in getList)
            {
                if (!item.checkBox.Checked)
                    continue;
                if(item.textBox.Text.Length >0)
                {
                    conditions.Add(new KeyValuePair<string, string>(item.column,item.textBox.Text));
                }
                columns.Add(item.column);
            }
            try
            {
                List<string[]> selectResult = sqliteConnecter.select(table, columns, conditions);
                updateDataGridView(selectResult);
            }catch(ConnectFailedException exp)
            {
                dataGridViewResult.Rows.Clear();
                dataGridViewResult.Columns.Clear();
                dataGridViewResult.Columns.Add(new DataGridViewTextBoxColumn());
                dataGridViewResult.Columns[0].HeaderText = exp.ToString();
            }
            catch(LoggerFailedException exp)
            {
                dataGridViewResult.Rows.Clear();
                dataGridViewResult.Columns.Clear();
                dataGridViewResult.Columns.Add(new DataGridViewTextBoxColumn());
                dataGridViewResult.Columns[0].HeaderText = exp.ToString();
            }
        }

        /// <summary>
        /// 更新信息显示表DataGridView中的内容，在点击
        /// </summary>
        /// <param name="resultList">列表首部数据集为属性名称，其它的为属性值</param>
        private void updateDataGridView(List<string[]> resultList)
        {
            dataGridViewResult.Rows.Clear();
            dataGridViewResult.Columns.Clear();
            bool isHead = true;
            int line = 0;
            foreach (string[] str in resultList)
            {
                if (isHead)
                {
                    isHead = false;
                    for (int i = 0; i < str.Length; i++)
                    {
                        dataGridViewResult.Columns.Add(new DataGridViewTextBoxColumn());
                        dataGridViewResult.Columns[i].HeaderText = str[i];
                        dataGridViewResult.Rows.Insert(0, resultList.Count);
                    }
                }
                else
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        dataGridViewResult.Rows[line].Cells[i].Value = str[i];
                    }
                    line++;
                }
            }
        }

        /// <summary>
        /// 当RadioBox更改后，修改当前状态和相关控件的可见性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonSelect_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Name.Equals("radioButtonSchool"))
            {
                panelSchoolSelect.Show();
                panelClassSelect.Hide();
                panelStudentSelect.Hide();
                radioChoosen = 0;
            }else if (((RadioButton)sender).Name.Equals("radioButtonClass"))
            {
                panelSchoolSelect.Hide();
                panelClassSelect.Show();
                panelStudentSelect.Hide();
                radioChoosen = 1;
            }else if (((RadioButton)sender).Name.Equals("radioButtonStudent"))
            {
                panelSchoolSelect.Hide();
                panelClassSelect.Hide();
                panelStudentSelect.Show();
                radioChoosen = 2;
            }
        }

        /// <summary>
        /// 将窗体控件按照功能类别进行分组
        /// </summary>
        private void checkBoxListInit()
        {
            this.listCheckBoxClass = new List<Item>();
            this.listCheckBoxSchool = new List<Item>();
            this.listCheckBoxStudent = new List<Item>();

            listCheckBoxSchool.Add(new Item(checkBoxAddress, "address", textBoxAddress));
            listCheckBoxSchool.Add(new Item(checkBoxBuildTime, "build_time", textBoxBuildTime));
            listCheckBoxSchool.Add(new Item(checkBoxHeadmaster, "headmaster", textBoxHeadmaster));
            listCheckBoxSchool.Add(new Item(checkBoxSchoolPhone, "school_phone", textBoxSchoolPhone));

            listCheckBoxClass.Add(new Item(checkBoxSchoolName_class, "school_name", textBoxSchoolName_class));
            listCheckBoxClass.Add(new Item(checkBoxMajority_class, "majority", textBoxMajority_class));
            listCheckBoxClass.Add(new Item(checkBoxGrade_class, "grade", textBoxGrade_class));
            listCheckBoxClass.Add(new Item(checkBoxTutor, "tutor", textBoxTutor));

            listCheckBoxStudent.Add(new Item(checkBoxGender, "gender", textBoxGender));
            listCheckBoxStudent.Add(new Item(checkBoxStuPhone, "stu_phone", textBoxStuPhone));
            listCheckBoxStudent.Add(new Item(checkBoxSchoolName_stu, "school_name", textBoxSchoolName_stu));
            listCheckBoxStudent.Add(new Item(checkBoxMajority_stu, "majority", textBoxMajority_stu));
            listCheckBoxStudent.Add(new Item(checkBoxGrade_stu, "grade", textBoxGrade_stu));
            listCheckBoxStudent.Add(new Item(checkBoxClass_stu, "class", textBoxClass_stu));
        }

    }
}
